package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.model.Page39Model;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @author Kzen
 * @Description description
 * @date 2020/10/16
 */
@Component
public class PageData39 {

    public String getData(ExcelModel model){
        List<Page39Model> page39s = model.getPage39s();
        Page39 page39 = new Page39();

        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < page39s.size(); i++) {
            Page39Model pageModel = page39s.get(i);
            if (pageModel.getThisName() != null && pageModel.getThisName().equals("本周")){
                list.add(i);
            }
        }

        List<Page39Model> sports = page39s.stream().skip(list.get(0)+1).limit(list.get(1) - list.get(0) - 2).collect(toList());
        List<Page39Model> youngs = page39s.stream().skip(list.get(1)+1).collect(toList());

        List<TempPage> sportPages = getTempPage(sports);
        List<TempPage> youngPages = getTempPage(youngs);

        page39.setTitle(LogicUtil.getTitle(model,39));
        page39.setSports(sportPages);
        page39.setYoungs(youngPages);
        page39.setDataPeriod(DateUtil.getLastWeekDate());

        String title2 = LogicUtil.getContent(model, 39, "title2");
        page39.setTitle2(getTitle2(title2));
        return JsonUtil.serialize(page39);
    }

    private List<String> getTitle2(String str){
        return new ArrayList<>(Arrays.asList(str.split("\n")));
    }

    private List<TempPage> getTempPage(List<Page39Model> sports) {
        List<TempPage> list = new ArrayList<>();
        for (Page39Model sport : sports) {
            TempPage tempPage = new TempPage();
            tempPage.setPhotoId(sport.getPhotoId());
            tempPage.setName1(sport.getThisName());
            tempPage.setName2(sport.getLastName());
            tempPage.setTrend(Integer.parseInt(sport.getTrend()));
            list.add(tempPage);
        }
        return list;
    }

    @Data
    private static class Page39{
        private String title;
        private List<String> title2;
        private String dataPeriod;
        private List<TempPage> sports;
        private List<TempPage> youngs;
    }

    @Data
    private static class TempPage{
        private String photoId;
        private String name1;
        private String name2;
        private int trend;
    }
}
